/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author eocandos
 */
public class DB extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ConnectionDB con = null;
        
        try {
            con = new ConnectionDB();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        //Boton presionado
        String button = request.getParameter("clicked");
        
        // Buscar datos (ok)
        if("Search".equals(button)){
            String table = request.getParameter("table");
            String id = request.getParameter("id");
            
            BeanLinks bean = con.buscar(table, id);
            
            request.setAttribute("bean", bean);
            request.getRequestDispatcher("updateLink.jsp").forward(request, response);
            //con.setUpdate("7","facebook","www.facebook.com","5/5/5","---","x","x");
            
        }
        // Actualizar datos
        if("Update".equals(button)){        
            
        String id = request.getParameter("id");    
        String name = request.getParameter("name");
        String url = request.getParameter("url");
        Calendar cal = Calendar.getInstance();
        int dia = cal.get(Calendar.DATE);
        int mes = cal.get(Calendar.MONTH);
        int anio = cal.get(Calendar.YEAR);
        String date
                = Integer.toString(dia) + "/" + Integer.toString(mes) + "/" + Integer.toString(anio);     
        String des = request.getParameter("desc");
        String pos = request.getParameter("pos");
        String tags = request.getParameter("tags");
        
        con.setUpdate(id,name,url,date,des,pos,tags);       
        
        //List<BeanLinks> links = con.consultar();
        //request.setAttribute("links", links);
        request.getRequestDispatcher("updateLink.jsp").forward(request, response);

      }
         // Guardar Nuevos (ok)
        if("SaveNew".equals(button)){        
        // All for Insert New Link
        String name = request.getParameter("name");
        String url = request.getParameter("url");
        Calendar cal = Calendar.getInstance();
        int dia = cal.get(Calendar.DATE);
        int mes = cal.get(Calendar.MONTH);
        int anio = cal.get(Calendar.YEAR);
        String date
                = Integer.toString(dia) + "/" + Integer.toString(mes) + "/" + Integer.toString(anio);     
        String des = request.getParameter("desc");
        String pos = request.getParameter("pos");
        String tags = request.getParameter("tags");

        con.insertDates(name, url, date, des, pos, tags);        
        request.getRequestDispatcher("newLink.jsp").forward(request, response);

        }
    }

}
